<?php
declare (strict_types=1);

namespace app\command;

use app\home\model\AmountLog;
use app\home\model\User;

use app\services\wechat\Offiaccount;
use think\console\Command;
use think\console\Input;
use think\console\Output;

/**
 * Class Coupon
 * 修改 失效优惠券
 */
class Recharge extends Command{
    protected function configure()
    {
        $this->setName('recharge')->setDescription('Recharge');
    }
    /**
     * execute()方法 就是 运行该命令行类的时候要执行的具体业务逻辑代码
     */
    protected function execute(Input $input, Output $output)
    {
        for ($i=0;$i<3;$i++) {
            $rechargeModel = new  \app\home\model\Recharge();
            $rechargeArray = $rechargeModel->where(['pay_status' => 1])->select()->toArray();
            $offiaccount = new Offiaccount();
            foreach ($rechargeArray as $value) {
                if ($value['type'] == 6) {
                    $rechargeInfo = $offiaccount->orderquery($value);
                    if ($rechargeInfo['return_code'] == 'SUCCESS') {
                        if ($rechargeInfo['trade_state'] == 'SUCCESS') {
                            if ($rechargeInfo['trade_state'] == 'SUCCESS') {
                                $rechargeModel->where(['id' => $value['id']])->update(['pay_status' => 2, 'status' => 3, 'update_time' => time()]);
                                $AmountLogData = [
                                    'uid' => $value['uid'],
                                    'amount' => $value['amount'],
                                    'create_time' => time(),
                                    'operate_type' => 6,
                                    'pay_type' => 1,
                                    'pay_method' => 1,
                                    'order_id' => $value['id'],
                                    'ratio' => 1,
                                    'type' => 1,
                                    'staff_id' => $value['staff_id'],
                                    'store_id' => $value['store_id'],
                                    'order_sn' => $value['sn_order'],
                                    'give_amount' => $value['give_amount'],
                                ];
                                $amountLogModel = new AmountLog();
                                $amountLogModel->insert($AmountLogData); // 余
                                if ($value['store_id']> 0){
                                    $storeModel = new \app\merchant\model\Store();
                                    $storeModel->where(['id' => $value['store_id']])->inc('recharge_amount',(float)$value['amount'])->update();
                                }
                                $userModel = new User();
                                $userModel->where(['id' => $value['uid']])
                                    ->inc('balance', (float)$value['total_amount'])
                                    ->inc('recharge_amount', (float)$value['amount'])
                                    ->update();
                            }
                        } elseif ($rechargeInfo['trade_state'] == 'CLOSED' || $rechargeInfo['trade_state'] == 'PAYERROR' || $rechargeInfo['trade_state'] == 'NOTPAY' || $rechargeInfo['trade_state'] == 'REFUND') {
                            if (time() - $value['pay_time'] > 120) {
                                $rechargeModel->where(['id' => $value['id']])->update(['pay_status' => 3]);
                            }

                        }
                    }
                } else {
                    $AliOffiaccount = new \app\services\alipay\Offiaccount();
                    $aliOrderInfo = $AliOffiaccount->orderquery($value);
                    if ($aliOrderInfo['alipay_trade_query_response']['code'] == 10000) {
                        if ($aliOrderInfo['alipay_trade_query_response']['trade_status'] == 'TRADE_SUCCESS') {
                            $rechargeModel->where(['id' => $value['id']])->update(['pay_status' => 2, 'status' => 3, 'update_time' => time()]);

                            $AmountLogData = [
                                'uid' => $value['uid'],
                                'amount' => $value['amount'],
                                'create_time' => time(),
                                'operate_type' => 7,
                                'pay_type' => 1,
                                'pay_method' => 2,
                                'order_id' => $value['id'],
                                'ratio' => 1,
                                'type' => 1,
                                'staff_id' => $value['staff_id'],
                                'store_id' => $value['store_id'],
                                'order_sn' => $value['sn_order'],
                                'give_amount' => $value['give_amount'],
                            ];
                            $amountLogModel = new AmountLog();
                            $amountLogModel->insert($AmountLogData); // 余
                            if ($value['store_id']> 0){
                                $storeModel = new \app\merchant\model\Store();
                                $storeModel->where(['id' => $value['store_id']])->inc('recharge_amount',(float)$value['amount'])->update();
                            }
                            $userModel = new User();
                            $userModel->where(['id' => $value['uid']])
                                ->inc('balance', (float)$value['total_amount'])
                                ->inc('recharge_amount', (float)$value['amount'])
                                ->update();
                        } elseif ($aliOrderInfo['alipay_trade_query_response']['trade_status'] == 'TRADE_CLOSED') {
                            $rechargeModel->where(['id' => $value['id']])->update(['pay_status' => 3]);
                        }
                    }
                }
            }
            sleep(20);
            $output->writeln('成功');

        }
    }
}